1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.
event.*;
4 import java.sql.*;
5 import java.io.*;

6 //import javafx.scene.paint.*;

7
8 class
MainFrame extends JFrame
9 {
10     Container c;
11     JButton btnAdd, btnView, btnUpdate , btnDelete;
12     Color col =
new Color(67,190,200);
13
14     MainFrame()
15     {
16         c = getContentPane();
17         c.setLayout(
new FlowLayout());
18         c.setBackground(
new Color(42, 119, 191));
19     btnAdd =
new JButton("Add");
20     btnAdd.setBackground(Color.orange);
21     btnView =
new JButton("View");
22     btnView.setBackground(Color.orange);
23     btnUpdate =
new JButton("Update");
24     btnUpdate.setBackground(Color.orange);
25     btnDelete =
new JButton("Delete");
26     btnDelete.setBackground(Color.orange);
27     
this.addWindowListener(new WL());
28
29     c.
add(btnAdd);
30     c.
add(btnView);
31     c.
add(btnUpdate);
32     c.
add(btnDelete);
33
34 btnAdd.addActionListener(
new ActionListener(){
35     
public void actionPerformed(ActionEvent ae)
36     {
37         AddFrame a =
new AddFrame();
38         dispose();
39     }
40 });
41
42 btnView.addActionListener(
new ActionListener(){
43     
public void actionPerformed(ActionEvent ae)
44     {
45         ViewFrame a =
new ViewFrame();
46         dispose();
47     }
48 });
49
50 btnUpdate.addActionListener(
new ActionListener(){
51     
public void actionPerformed(ActionEvent ae){
52         UpdateFrame a =
new UpdateFrame();
53         dispose();
54     }
55 });
56
57 btnDelete.addActionListener(
new ActionListener(){
58     
public void actionPerformed(ActionEvent ae){
59         DeleteFrame a =
new DeleteFrame();
60         dispose();
61
62     }
63 });
64
65
66
67     setTitle(
"S. M. S");
68     setSize(
500,400);
69     setLocationRelativeTo(
null);
70     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
71     setVisible(
true);
72     }
73
74
75     
public static void main(String[] args)
76     {
77         MainFrame m =
new MainFrame();
78
79     }

80
81
82 class
WL implements WindowListener {
83
84     
public void windowActivated(WindowEvent e) {}
85     
public void windowDeactivated(WindowEvent e) {}
86
87     
public void windowOpened(WindowEvent e)
88     {
89         JOptionPane.showMessageDialog(c ,
"Welcome to Student Management System ");
90     }
91
92     
public void windowClosed(WindowEvent e) {}
93
94     
public void windowClosing(WindowEvent e)
95     {
96         
int output = JOptionPane.showConfirmDialog(c , "Are you Sure ? ", null , JOptionPane.YES_OPTION);
97         
if(output == JOptionPane.YES_OPTION)
98         {
99             JOptionPane.showMessageDialog(
null , "Please visit again");
100             System.exit(
1);
101         }
102          
103
104     }
105
106     
public void windowIconified(WindowEvent e) {}
107     
public void windowDeiconified(WindowEvent e) {}
108
109 }
// end of WL
110
111 }

112
113
114 class
DbHandler
115 {
116     
117     
public void addStudent(int rno , String name)
118     {
119         
try
120         {
121             
/*DriverManager.registerDriver
122             (
new oracle.jdbc.driver.OracleDriver());*/
123             Connection con = DriverManager.getConnection
124             (
"jdbc:mysql://localhost:3306/db2","root","mysqlmysqli0");
125             String sql =
"insert into student values(?,?)";
126             PreparedStatement pst = con.prepareStatement(sql);
127             pst.setInt(
1,rno);
128             pst.setString(
2,name);
129             
int r =pst.executeUpdate();
130             JOptionPane.showMessageDialog(
new JDialog() , r + " records inserted");
131             con.close();
132
133         }
134         
catch(SQLException e)
135         {
136             JOptionPane.showMessageDialog(
new JDialog(),"ii" +e);
137         }
138     }
139
140     
public String viewStudent()
141     {
142         StringBuffer sb =
new StringBuffer();
143         
try
144         {
145             
/*DriverManager.registerDriver
146             (
new oracle.jdbc.driver.OracleDriver());*/
147             Connection con = DriverManager.getConnection
148             (
"jdbc:mysql://localhost:3306/db2","root","mysqlmysqli0");
149             String sql =
"select * from Student";
150             Statement stmt = con.createStatement();
151             ResultSet rs = stmt.executeQuery(sql);
152
153             
while(rs.next()){
154                 
int rno = rs.getInt(1);
155                 String name = rs.getString(
2);
156                 sb.append(
"Rno " + rno + " Name " + name + "\n");
157             }
158             rs.close();
159             con.close();
160
161         }
162         
catch(SQLException e)
163         {
164             
165         }
166         
return sb.toString();
167     }
168
169     
public void UpdateStudent(int rno ,String name)
170     {
171         
try
172         {
173             
/*DriverManager.registerDriver
174             (
new oracle.jdbc.driver.OracleDriver());*/
175             Connection con = DriverManager.getConnection
176             (
"jdbc:mysql://localhost:3306/db2","root","mysqlmysqli0");
177             String sql1 =
"update student set name=? where rno=? ";
178             PreparedStatement pstmt1 = con.prepareStatement(sql1);
179             pstmt1.setInt(
2, rno);
180             pstmt1.setString(
1,name);
181             
int z = pstmt1.executeUpdate();
182             JOptionPane.showMessageDialog(
new JDialog(),z + " Name Updated" );
183             con.close();
184
185
186         }
187         
catch(SQLException e)
188         {
189             System.
out.println("Update issue" + e);
190         }
191     }
192     
public void DeleteStudent(int rno)
193     {
194         
try
195         {
196             
/*DriverManager.registerDriver
197             (
new oracle.jdbc.driver.OracleDriver());*/
198             Connection con = DriverManager.getConnection
199             (
"jdbc:mysql://localhost:3306/db2","root","mysqlmysqli0");
200             String s2 =
"delete from student where rno=?";
201             PreparedStatement p1 = con.prepareStatement(s2);
202             p1.setInt(
1,rno);
203             
int r = p1.executeUpdate();
204             JOptionPane.showMessageDialog(
new JDialog(), r + " Student Data Deleted ");
205             con.close();
206
207
208         }
209         
catch(SQLException e)
210         {
211             System.
out.println("Delete issue" + e);
212         }
213     
214
215
216     }
217
218 }
// end of DBHandler class
219
220
221 /*
222 import javax.swing.*;
223 import java.awt.*;
224 import java.awt.
event.*;
225 import java.sql.*;

226
227
228 class
MainFrame extends JFrame
229 {
230     Container c;
231     JButton btnAdd, btnView, btnUpdate , btnDelete;
232
233     MainFrame()
234     {
235         c = getContentPane();
236         c.setLayout(
new FlowLayout());
237     btnAdd =
new JButton("Add");
238     btnView =
new JButton("View");
239     btnUpdate =
new JButton("Update");
240     btnDelete =
new JButton("Delete");
241
242     c.
add(btnAdd);
243     c.
add(btnView);
244     c.
add(btnUpdate);
245     c.
add(btnDelete);
246
247 btnAdd.addActionListener(
new ActionListener(){
248     
public void actionPerformed(ActionEvent ae)
249     {
250         AddFrame a =
new AddFrame();
251         dispose();
252     }
253 });
254
255 btnView.addActionListener(
new ActionListener(){
256     
public void actionPerformed(ActionEvent ae)
257     {
258         ViewFrame a =
new ViewFrame();
259         dispose();
260     }
261 });
262
263 btnUpdate.addActionListener(
new ActionListener(){
264     
public void actionPerformed(ActionEvent ae){
265         UpdateFrame a =
new UpdateFrame();
266         dispose();
267     }
268 });
269
270
271     setTitle(
"S. M. S");
272     setSize(
350,200);
273     setLocationRelativeTo(
null);
274     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
275     setVisible(
true);
276     }
277
278
279     
public static void main(String[] args)
280     {
281         MainFrame m =
new MainFrame();
282     }
283
284 }

285
286
287 class
DbHandler
288 {
289     
public void addStudent(int rno , String name)
290     {
291         
try
292         {
293             DriverManager.registerDriver
294             (
new oracle.jdbc.driver.OracleDriver());
295             Connection con = DriverManager.getConnection
296             (
"jdbc:oracle:thin:@localhost:1521:xe","system","abc123");
297             String sql =
"insert into student values(?,?)";
298             PreparedStatement pst = con.prepareStatement(sql);
299             pst.setInt(
1,rno);
300             pst.setString(
2,name);
301             
int r =pst.executeUpdate();
302             JOptionPane.showMessageDialog(
new JDialog() , r + "records inserted");
303             con.close();
304
305         }
306         
catch(SQLException e)
307         {
308             JOptionPane.showMessageDialog(
new JDialog(),"ii" +e);
309         }
310     }
311
312     
public String viewStudent()
313     {
314         StringBuffer sb =
new StringBuffer();
315         
try
316         {
317             DriverManager.registerDriver
318             (
new oracle.jdbc.driver.OracleDriver());
319             Connection con = DriverManager.getConnection
320             (
"jdbc:oracle:thin:@localhost:1521:xe","system","abc123");
321             String sql =
"select * from Student";
322             Statement stmt = con.createStatement();
323             ResultSet rs = stmt.executeQuery(sql);
324
325             
while(rs.next()){
326                 
int rno = rs.getInt(1);
327                 String name = rs.getString(
2);
328                 sb.append(
"Rno " + rno + "Name " + name + "\n");
329             }
330             rs.close();
331             con.close();
332
333         }
334         
catch(SQLException e)
335         {
336             
337         }
338         
return sb.toString();
339     }
340
341     
public void UpdateStudent()
342     {
343         
try
344         {
345             DriverManager.registerDriver
346             (
new oracle.jdbc.driver.OracleDriver());
347             Connection con = DriverManager.getConnection
348             (
"jdbc:oracle:thin:@localhost:1521:xe","system","abc123");
349             Statement stmt1 = con.createStatement();
350             String sql1 =
"update student set name='Harshal' where rno=4 ";
351             
int z = stmt1.executeUpdate(sql1);
352             JOptionPane.showMessageDialog(
new JDialog(),z + "Name Updated" );
353             con.close();
354
355
356         }
357         
catch(SQLException e)
358         {
359             System.
out.println("Update issue" + e);
360         }
361
362
363
364     }
365
366 } // end of DBHandler
class
367 */


Gõ tìm kiếm nhanh...